﻿using Core.Proxy.Attributes.InterceptorAttributes.BaseAttributes;
using Core.Proxy.Enums;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Core.Proxy.Attributes.InterceptorAttributes
{
    /// <summary>
    /// 缓存锁特性
    /// </summary>
    [AttributeUsage(AttributeTargets.Method, AllowMultiple = false, Inherited = false)]
    public class CacheLockAttribute : BaseCacheAttribute
    {
        /// <summary>
        /// 获取锁失败后重试间隔
        /// </summary>
        public int RetryInterval { get; }
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="constKey">常量key</param>
        /// <param name="maxLockMillisecond">最大锁定时间</param>
        /// <param name="retryInterval">获取锁失败后重试间隔</param>
        /// <param name="cacheKeyPrefix">锁的key的前缀,ClassName-类名,ClassNameAndMethodName-类名和方法名,None-没有前缀</param>
        /// <param name="dynamicKey">变量keylist(此key指参数)。如果需要使用参数的值，声明格式如下:{index}({}不需要写,只是说明这是占位符)。如果参数是一个引用类型,需要使用其中的某个属性，声明格式如下:{index}.{propertyName}({}不需要写,只是说明这是占位符),多个key用','分割</param>
        public CacheLockAttribute(string constKey, int maxLockMillisecond = 5000, int retryInterval = 1000, CacheKeyPrefix cacheKeyPrefix = CacheKeyPrefix.None, string dynamicParamKeys = null)
        {
            ConstKey = constKey;
            ExpireMillisecond = maxLockMillisecond;
            RetryInterval = retryInterval;
            CacheKeyPrefix = cacheKeyPrefix;
            if (dynamicParamKeys != null)
            {
                DynamicParamKeys = dynamicParamKeys.Split(',').ToList();
            }
        }
    }
}
